/**
 * 
 * The Field class represents the field with all the bricks on which the game is being played
 * It holds a list of all the bricks and generates a random field when the game is launched.
 * 
 * Author: Matthias Van Eeghem <matthias@van-eeghem.com>
 * All rights reserved.
 * 
 */

package com.bomberman.core;

import java.util.Random;

import com.bomberman.brick.Brick;
import com.bomberman.brick.BrickManager;

public class Field 
{
	/**
	 * This method generates a random brick field
	 */
	public static void generate()
	{
		// Get a random generator
		Random randomGenerator = new Random();

		// Loop through all rows
		for(int i = 0; i < Game.AMOUNT_OF_ROWS; i++)
		{
			// Now loop through all colums
			for(int j = 0; j < Game.AMOUNT_OF_COLUMNS; j++)
			{
				// Are we allowed to potentially build a brick on that location?
				// We might not since we don't want to build anything in the first
				// few bricks of each corner of the field, the players are supposed to spawn there.
				if(Field.allowedToBuildBrickAtLocation(i, j))
				{
					// Create a random number from zero through 4
					int randomInt = randomGenerator.nextInt(5);
					
					// We create a brick 4 of the 5 times, we want to keep a few gaps open as well.
					if(randomInt != 4)
					{
						// Add the brick
						BrickManager.addBrick(new Brick(i, j));
					}
				}
			}
		}
	}
	
	/**
	 * Return the field as a string
	 * 
	 * @return The format of the string "FIELD 0,0 1,0" for a field with two bricks, one at (0, 0) and one at (1, 0)
	 */
	public static String getFieldAsString()
	{
		String fieldAsString = "FIELD ";
		
		// Loop through all bricks and append them to the string
		for(Brick brick : BrickManager.getBrickList())
		{
			fieldAsString += brick.getRow() + "," + brick.getColumn() + " ";
		}
		
		return fieldAsString;
	}
	
	/**
	 * This method returns true if we're allowed to build a brick on the location marked by
	 * row and column, false otherwise. We do not want to build any bricks in the 4 corners of the field
	 * and the few surrounding bricks, that's where the players will spawn. 
	 */
	private static boolean allowedToBuildBrickAtLocation(int row, int column)
	{
		// Corner 1: {0, 0}, {0, 1}, {1, 0}
		if(row == 0 && column == 0) return false;
		if(row == 0 && column == 1) return false;
		if(row == 1 && column == 0) return false;
		
		// Corner 2: {15, 0}, {15, 1}, {14, 0}
		if(row == (Game.AMOUNT_OF_ROWS - 1) && column == 0) return false;
		if(row == (Game.AMOUNT_OF_ROWS - 1) && column == 1) return false;
		if(row == (Game.AMOUNT_OF_ROWS - 2) && column == 0) return false;
		
		// Corner 3: {0, 18}, {0, 19}, {1, 19}
		if(row == 0 && column == (Game.AMOUNT_OF_COLUMNS - 2)) return false;
		if(row == 0 && column == (Game.AMOUNT_OF_COLUMNS - 1)) return false;
		if(row == 1 && column == (Game.AMOUNT_OF_COLUMNS - 1)) return false;
		
		// Corner 4: {15, 18}, {15, 19}, {15, 19}
		if(row == (Game.AMOUNT_OF_ROWS - 1) && column == (Game.AMOUNT_OF_COLUMNS - 2)) return false;
		if(row == (Game.AMOUNT_OF_ROWS - 1) && column == (Game.AMOUNT_OF_COLUMNS - 1)) return false;
		if(row == (Game.AMOUNT_OF_ROWS - 2) && column == (Game.AMOUNT_OF_COLUMNS - 1)) return false;
		
		return true;
	}
}
